
<?php

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

require("frmCookie.php");

$iOpcion = $_GET[iOpcion];

switch ($iOpcion)
{
	case 1:
	{
		$mySQL = "SELECT S.idStock FROM stock S, partes P WHERE S.idParte = P.idParte AND S.idAlmacen=" . $_POST[txtOrigenDef];
		$result = readSQL($mySQL);

        foreach($result as $value)
        {
        	$field = "txt" . $value[0] . "hasta";	
        	
        	if($_POST[$field] != "")
        	{
        		$mySQLStock = "SELECT * FROM stock WHERE idStock=" . $value[0];
        		$resultStock = readSQL($mySQLStock);
        		
        		foreach($resultStock as $valueStock)
        		{
        			$mySQLStockTran = "INSERT INTO stockentransito(idParte, idAlmacenOrigen, idAlmacen, idMoneda, imei, nroFactura, codigoReferencia, swVersion, hwVersion, precio, cantidad, fecha, idUsuario, nroRemito, idOrdenCompra, idProveedor, nroRemitoLlegada, simlock) VALUES(";
        			$mySQLStockTran = $mySQLStockTran . $valueStock['idParte'] . ", ";
        			$mySQLStockTran = $mySQLStockTran . $_POST[txtOrigenDef] . ", ";
        			$mySQLStockTran = $mySQLStockTran . $_POST[txtDestinoDef] . ", ";
        			$mySQLStockTran = $mySQLStockTran . "'" . $valueStock['idMoneda'] . "', ";
        			$mySQLStockTran = $mySQLStockTran . "'" . $valueStock['imei'] . "', ";
        			$mySQLStockTran = $mySQLStockTran . "'" . $valueStock['nroFactura'] . "', ";
        			$mySQLStockTran = $mySQLStockTran . "'" . $valueStock['codigoReferencia'] . "', ";
        			$mySQLStockTran = $mySQLStockTran . "'" . $valueStock['swVersion'] . "', ";
        			$mySQLStockTran = $mySQLStockTran . "'" . $valueStock['hwVersion'] . "', ";
        			$mySQLStockTran = $mySQLStockTran . "'" . $valueStock['precio'] . "', ";
        			$mySQLStockTran = $mySQLStockTran . $_POST[$field] . ", ";
        			$mySQLStockTran = $mySQLStockTran . "'" . $valueStock['fecha'] . "', ";
        			$mySQLStockTran = $mySQLStockTran . $_SESSION['idUsuario'] . ", ";
        			$mySQLStockTran = $mySQLStockTran . "'" . $_POST[txtRemitoDef] . "', ";
					$mySQLStockTran = $mySQLStockTran . "'" . $valueStock['idOrdenCompra'] . "', ";
					$mySQLStockTran = $mySQLStockTran . "'" . $valueStock['idProveedor'] . "', ";
					$mySQLStockTran = $mySQLStockTran . "'" . $valueStock['nroRemitoLlegada'] . "', ";
					$mySQLStockTran = $mySQLStockTran . "'" . $valueStock['simlock'] . "')";
        			//echo $mySQLStockTran . "<br>";
					//exit;
        			executeSQL($mySQLStockTran);
        			
        			$mySQLSelectStock = "SELECT cantidad FROM stock WHERE idStock=" . $value[0];
            		$resultSelectStock = readSQL($mySQLSelectStock);
					foreach($resultSelectStock as $valueSelectStock)
					{
						$total = $valueSelectStock[0] - $_POST[$field];
						$mySQLUpdateStock = "UPDATE stock SET cantidad = " . $total . " WHERE idStock = " . $value[0];
						//echo $mySQLUpdateStock . "<br>";
						executeSQL($mySQLUpdateStock);
					}
        			
        			$mySQLMax = "SELECT MAX(idStockEnTransito) FROM stockentransito";
					$resultMax = readSQL($mySQLMax);
					foreach($resultMax as $valueMax)
					{
						$idStockEnTransito = $valueMax[0];
					}
        			
        			$mySQLUsos = "SELECT * FROM tiposusosxstock WHERE idStock=" . $value[0];
            		$resultUsos = readSQL($mySQLUsos);
            		foreach($resultUsos as $valueUsos)
            		{
            			$mySQLUsosTran = "INSERT INTO tiposusosxstockentransito VALUES(";
            			$mySQLUsosTran = $mySQLUsosTran . $valueUsos['idTipoUso'] . ", ";
            			$mySQLUsosTran = $mySQLUsosTran . $idStockEnTransito . ", ";
            			$mySQLUsosTran = $mySQLUsosTran . $valueUsos['idParte'] . ", ";
            			$mySQLUsosTran = $mySQLUsosTran . "'" . $valueUsos['imei'] . "', ";
            			$mySQLUsosTran = $mySQLUsosTran . "'" . $valueUsos['nroFactura'] . "')";
            			executeSQL($mySQLUsosTran);
            			echo $mySQLUsosTran . "<br>";
            		}
        		}
        	}
        }
		
		//exit;
        header("Location: movimientos.php?iOpcion=1&iMensaje=1");
		break;
	}
}
?>
